package cn.edu.neusoft.food729.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.retroiftdemo_729.R;
import com.squareup.picasso.Picasso;

import java.util.List;

import cn.edu.neusoft.food729.Bean.UserCollectionBean;
import cn.edu.neusoft.food729.activity.FoodInfoActivity;
import cn.edu.neusoft.food729.common.Common;
import cn.edu.neusoft.food729.listener.CollectListener;
import cn.edu.neusoft.food729.model.CollectModel;


public class FoodCollectionAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<UserCollectionBean> list;
    private Context context;
    private LayoutInflater layoutInflater;

    public FoodCollectionAdapter(Context context) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
    }

    public void setList(List<UserCollectionBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = layoutInflater.inflate(R.layout.collect_item, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final UserCollectionBean demoBean = list.get(position);
        if (demoBean == null) return;
        ViewHolder viewholder = (ViewHolder) holder;
        Picasso.with(context)
                .load(Common.BASEURL + demoBean.getPic())
                .placeholder(R.mipmap.ic_launcher)
                .into(viewholder.imageView);
        viewholder.name.setText(demoBean.getFoodname());
//        viewholder.price.setText((int) demoBean.getPrice());


        viewholder.enter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, FoodInfoActivity.class);
                intent.putExtra("food_id", list.get(position).getFood_id());
                intent.putExtra("food_name", list.get(position).getFoodname());
                intent.putExtra("image", list.get(position).getPic());
                intent.putExtra("price", list.get(position).getPrice());
//                intent.putExtra("intro",list.get(position).getIntro());
                context.startActivity(intent);
            }
        });

        viewholder.cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CollectModel collectModel = new CollectModel();
                CollectListener collectListener = new CollectListener() {
                    @Override
                    public void onResponse(String msg) {
                    }

                    @Override
                    public void onFail(String msg) {
                    }
                };
                collectModel.uncollectFood(String.valueOf(demoBean.getUser_id()), demoBean.getFood_id(), collectListener);
//                notifyDataSetChanged();
            }
        });
    }

    @Override
    public int getItemCount() {
        if (list != null)
            return list.size();
        else
            return 0;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView name, price;
        Button enter, cancel;

        public ViewHolder(View itemView) {
            super(itemView);

            imageView = itemView.findViewById(R.id.imageView8);
            name = itemView.findViewById(R.id.name);
            price = itemView.findViewById(R.id.address);

            enter = itemView.findViewById(R.id.button9);
            cancel = itemView.findViewById(R.id.button10);
        }
    }

}
